package com.bawei.play.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.common.mvi.BaseViewModel
import com.bawei.play.VideoPlayUiState
import com.bawei.play.VideoPlayViewIntent
import com.bawei.play.repos.PlayRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class VideoPlayViewModel @Inject constructor(private val repository: PlayRepository): BaseViewModel<VideoPlayViewIntent>() {
    private val _uiState = MutableStateFlow<VideoPlayUiState>(VideoPlayUiState.LoadEmpty)
    val uiState: StateFlow<VideoPlayUiState> = _uiState

    override fun handIntent(uiIntent: VideoPlayViewIntent) {
        when (uiIntent) {
            is VideoPlayViewIntent.LoadRecommendSimpleVideo -> loadRecommendSimpleVideo(
                uiIntent.page,
                uiIntent.pageSize
            )

            is VideoPlayViewIntent.LoadSimpleVideoByChannelId -> loadSimpleVideoByChannelId(
                uiIntent.channelId,
                uiIntent.page,
                uiIntent.pageSize
            )

            is VideoPlayViewIntent.LoadVideoCommends -> loadVideoCommends(
                uiIntent.dataType,
                uiIntent.itemId
            )
        }

    }

    private fun loadVideoCommends(dataType: Int, itemId: String) {
        viewModelScope.launch {
            try {
                _uiState.value = VideoPlayUiState.LoadEmpty
                val result = repository.getVideoCommends(dataType, itemId)
                if (result.code == 0 && result.data.isNotEmpty()) {
                    _uiState.value = VideoPlayUiState.LoadVideoCommendSuccess(result.data)
                } else {
                    _uiState.value = VideoPlayUiState.LoadVideoCommendFail(result.msg)
                }
            } catch (e: Exception) {
                _uiState.value = VideoPlayUiState.LoadVideoCommendFail("加载数据异常！")
            }
        }
    }

    private fun loadSimpleVideoByChannelId(channelId: String, page: Int, pageSize: Int) {
        viewModelScope.launch {
            _uiState.value = VideoPlayUiState.LoadEmpty
            repository.getVideoByChannelId(channelId, page, pageSize)
                .catch {
                    _uiState.value = VideoPlayUiState.LoadSimpleVideoFail("加载数据异常！")
                }
                .collect {
                    if (it.isNotEmpty()) {
                        _uiState.value = VideoPlayUiState.LoadSimpleVideoSuccess(it)
                    } else {
                        _uiState.value = VideoPlayUiState.LoadSimpleVideoFail("暂时没有数据！")
                    }
                }
        }
    }

    private fun loadRecommendSimpleVideo(page: Int, pageSize: Int) {
        viewModelScope.launch {
            try {
                _uiState.value = VideoPlayUiState.LoadEmpty
                val result = repository.getRecommendVideo(page, pageSize)
                if (result.code == 0 && result.data.isNotEmpty()) {
                    _uiState.value = VideoPlayUiState.LoadSimpleVideoSuccess(result.data)
                } else {
                    _uiState.value = VideoPlayUiState.LoadSimpleVideoFail(result.msg)
                }
            } catch (e: Exception) {
                _uiState.value = VideoPlayUiState.LoadSimpleVideoFail("加载数据异常！")
            }
        }
    }


}